/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

import { DefaultTask, hvigorCore, HvigorCoreNode, HvigorLogger } from '@ohos/hvigor';
import { generateReport } from '@ohos/hvigor-arkts-compose';

import { coverageCollectConst } from '../../const/coverageCollect-const.js';
import { TaskNames } from '../common/task-names.js';
import { TaskService } from '../service/task-service.js';
import task = TaskNames.CommonTask;

const LOG = HvigorLogger.getLogger('coverageCollect');

const PROJECT_PATH = coverageCollectConst.PROJECT_PATH;
const REPORT_PATH = coverageCollectConst.REPORT_PATH;
const COVERAGE_FILE = coverageCollectConst.COVERAGE_FILE;

/**
 * hvigor coverageCollect命令 任务
 *
 * @since 2023/8/25
 */
export class CoverageCollect extends DefaultTask {
  private readonly projectPath: string | undefined;

  private readonly reportPath: string | undefined;

  private readonly coverageFile: string | undefined;

  private readonly _taskService: TaskService;

  constructor(node: HvigorCoreNode, taskService: TaskService) {
    super(node, task.COLLECT_COVERAGE);
    this._taskService = taskService;
    this.projectPath = hvigorCore.getExtraConfig().get(PROJECT_PATH);
    this.reportPath = hvigorCore.getExtraConfig().get(REPORT_PATH);
    this.coverageFile = hvigorCore.getExtraConfig().get(COVERAGE_FILE);
  }

  registryAction(): Function {
    return async () => {
      LOG.info('Generate coverage statistics reports based on instrumentation dotting data');
      await generateReport(this.projectPath!, this.reportPath!, this.coverageFile!);
    };
  }
}
